
from django.http import Http404
from django.utils import timezone
from rest_framework import mixins
from rest_framework.generics import GenericAPIView, ListAPIView
from rest_framework.permissions import IsAdminUser
from rest_framework.response import Response
from rest_framework.views import APIView

from goods.models import GoodsVisitCount
from meiduo_admin.serializers.statistical import GoodsVisitSerializer
from users.models import User


class UserTotalCountView(APIView):
    # 用户认证
    permission_classes = [IsAdminUser]
    """用户总数统计"""
    def get(self, request):
        try:
            count = User.objects.count()
        except User.DoesNotExist:
            raise Http404
        now_date = timezone.now()

        response_data = {
            # 只需要返回：年-月-日
            'date': now_date.date(),
            'count': count
        }
        return Response(response_data)


class UserDayIncrementView(APIView):
    """日增用户统计"""
    permission_classes = [IsAdminUser]
    def get(self, request):
        now_date = timezone.now().replace(hour=0, minute=0, second=0, microsecond=0)
        try:
            count = User.objects.filter(date_joined__gte=now_date).count()
        except User.DoesNotExist:
            raise Http404
        response_data = {
            'date': now_date.date(),
            'count': count
        }
        return Response(response_data)


class UserDayActiveView(APIView):
    """日活跃用户统计"""
    permission_classes = [IsAdminUser]
    def get(self, request):
        now_date = timezone.now().replace(hour=0, minute=0, second=0, microsecond=0)
        try:
            count = User.objects.filter(last_login__gte=now_date).count()
        except User.DoesNotExist:
            raise Http404

        response_data = {
            'date': now_date.date(),
            'count': count
        }
        return Response(response_data)


class UserDayOrderView(APIView):
    """日下单用户数量"""
    permission_classes = [IsAdminUser]
    def get(self, request):
        now_date = timezone.now().replace(hour=0, minute=0, second=0, microsecond=0)
        try:
            count = User.objects.filter(orders__create_time__gte=now_date).distinct().count()  # distinct 去重（同一个用户下的两个单算一个用户量）
        except User.DoesNotEXist:
            raise Http404

        response_data = {
            'date': now_date,
            'count': count
        }

        return Response(response_data)


class UserMouthCountView(APIView):
    """获取当月每日新增用户数据"""
    permission_classes = [IsAdminUser]

    def get(self, request):
        end_date = timezone.now().replace(hour=0, minute=0, second=0, microsecond=0)
        start_date = end_date - timezone.timedelta(days=29)

        current_date = start_date

        # 新增用户的数量
        mouth_li = []

        while current_date <= end_date:
            next_day = current_date + timezone.timedelta(days=1)

            count = User.objects.filter(date_joined__gte=current_date, date_joined__lt=next_day).count()
            mouth_li.append({
                'date': current_date.date(),
                'count': count
            })
            current_date = next_day

        # 返回应答
        return Response(mouth_li)


class GoodsDayView(ListAPIView):
    """当日分类商品访问量"""
    permission_classes = [IsAdminUser]

    serializer_class = GoodsVisitSerializer

    def get_queryset(self):
        now_date = timezone.now().date()
        goods_visit = GoodsVisitCount.objects.filter(date=now_date)
        return goods_visit

    pagination_class = None

    # def get(self, request):
    #     return list(request)

    # def get(self, request):
    #     goods_visit = self.get_queryset()
    #
    #     serializer = self.get_serializer(goods_visit, many=True)
    #
    #     return Response(serializer.data)

